<?php

class db_engine_mysql implements iDBControllerBackend {

	private static $a = array ();
	
	private
		$status      = false,
		$transaction = false;
		
	public function __construct () {}
	
	public function __destruct () {
		mysql_close ();
	}
	
	public function query ($sQuery, &$insertId = 0) {
		$res = mysql_query ($sQuery);
		$err = mysql_error ();
		
		if ($err) {
			if ($this->transaction) {
				mysql_query ("ROLLBACK");
				$this->transaction = false;
			}
			throw new MySQLException (lang('error_mysql_query','DBController') . $err, $sQuery);
		}
		
		$insertId = mysql_insert_id ();
		return $res;
	}
	
	public function fetch_row ($resourse) {
		return mysql_fetch_row ($resourse);
	}
	
	public function fetch_array ($resourse) {
		return mysql_fetch_assoc ($resourse);
	}
	
	public function num_rows ($resourse) {
		return mysql_num_rows ($resourse);
	}
	
	public function reset_pointer ($resourse) {
		return mysql_data_seek ($resourse, 0);
	}
	
	public function connect () {
		if ($this->status) {
			return false;
		}
		
		if (!$mysqlconnect = mysql_connect (DB_SERVER, DB_USER, DB_PASSWORD)) {
			throw new MySQLException (lang ('error_server_connection_failed','db'));
		}
			
		if (!mysql_select_db (DB_NAME, $mysqlconnect)) {
			throw new MySQLException (lang ('error_db_connection_failed','db'));
		}
		
		if (version_compare(mysql_get_server_info(), '4.1', ">=")) {
			mysql_query("/*!40101 SET NAMES 'utf8' */");
		}
	}
	
	public function startTransaction () {
		$this->query ("SET AUTOCOMMIT=0");
		$this->query ("START TRANSACTION");
		$this->transaction = true;
	}
	
	public function stopTransaction () {
		if ($this->transaction) {
			$this->query ("COMMIT");
			$this->transaction = false;
		}
	}

}